<template>
	<view>
		<view class="" v-if="type==1">
			<view v-show="times==1">
				<view class="parperbox_top_item word-box-hover d-b-c" :class="{'slide_out':item.slide}" v-for="(item, index) in list" :key="index" v-if="item.show>0" @click="playAudio(item.audio_url)">
					<view class="f36 fb" v-if="!item.change">{{item.word}}</view>
					<view class="f36 fb" v-if="item.change">{{item.chinese}}</view>
					<view class="cropp_icon" @click.stop="checkWord(index, 0)">
						<image src="/static/index1/59.png" mode=""></image>
					</view>
				</view>
				<!-- 底部 -->
				<view class="parperbox_bot">
					<view class="parperbox_bot_box d-b-c">
						<view class="parperbox_bot_box_img" style="position: relative;">
							<image src="/static/index1/60.png" mode=""></image>
							<u-badge :count="count0" :offset="[0, 0]"></u-badge>
						</view>
					</view>
				</view>
			</view>
			<view v-show="times==2">
				<view class="parperbox_top">
					<view class="parperbox_top_item word-box-hover d-b-c" :class="{'slide_out': item.slide}" v-for="(item, index) in list" :key="index" v-if="item.show>0" @click="playAudio(item.audio_url)">
						<view class="f36 fb">{{item.word}}</view>
						<view class="d-s-c parperbox_top_item_yes">
							<image src="/static/index1/59.png" mode="widthFix" @click.stop="checkWord(index, 1)"></image>
							<image src="/static/index1/61.png" mode="widthFix" @click.stop="checkWord(index, 2)"></image>
						</view>
					</view>
				</view>
				<!-- 底部 -->
				<view class="parperbox_bot">
					<view class="parperbox_bot_box d-b-c">
						<view class="parperbox_bot_box_img" style="position: relative;">
							<image src="/static/index1/60.png" mode=""></image>
							<u-badge :count="count1" :offset="[0, 0]"></u-badge>
						</view>
						<view class="parperbox_bot_box_img" style="position: relative;" @click="wrongAgain">
							<image src="/static/index1/62.png" mode=""></image>
							<u-badge :count="count2" :offset="[0, 0]"></u-badge>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="delection-box d-c-c" v-if="type==2">
			<view class="box" @click="recheck">
				<image src="/static/index1/63.png" mode="heightFix"></image>
				<view class="box-text text1 d-c-c">
					<image src="/static/index1/66.png" mode="heightFix"></image>
					<text>重新检查</text>
				</view>
			</view>
			<view class="box" @click="mixtureShow=true" v-if="boxLastPage>1">
				<image src="/static/index1/64.png" mode="heightFix"></image>
				<view class="box-text text2 d-c-c">
					<image src="/static/index1/67.png" mode="heightFix"></image>
					<text>混组检查</text>
				</view>
			</view>
			<view class="box" @click="nextPage" v-if="boxLastPage>boxPage">
				<image src="/static/index1/65.png" mode="heightFix"></image>
				<view class="box-text text2 d-c-c">
					<image src="/static/index1/68.png" mode="heightFix"></image>
					<text>下一组</text>
				</view>
			</view>
			<view class="box" @click="endStudy" v-if="boxLastPage==boxPage">
				<image src="/static/index1/65.png" mode="heightFix"></image>
				<view class="box-text text2 d-c-c">
					<image src="/static/index1/68.png" mode="heightFix"></image>
					<text>结束识记</text>
				</view>
			</view>
			<template>
				<view>
					<u-popup v-model="mixtureShow" mode="bottom" :mask="popupMask" border-radius="60" :custom-style="popupCustomStyle">
						<view class="">
							<view class="parperbox_age_til">请选择您要混合的单词组</view>
							<scroll-view scroll-y style="width:100%;min-height:20vh;padding:30rpx 0;">
								<view class="parperbox_bot_box">
									<view class="parperbox_bot_box_image" v-for="(item, index) in boxData" :key="index" @click="checkBox(index)">
										<image src="/static/curr_yes.png" mode="" v-if="!item.check"></image>
										<image src="/static/curr_icn_check.png" mode="" v-if="item.check"></image>
									</view>
								</view>
							</scroll-view>
							<view class="parperbox_age_but" @click="startCheck">开始检测</view>
						</view>
					</u-popup>
				</view>
			</template>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgUrl: getApp().globalData.imgUrl,
				studyId: 0,
				boxPage: 0,
				boxLastPage: 0,
				boxData: [],
				type: 1,
				times: 1,
				mixtureShow: false,
				wordsList: [],
				list: [],
				count0: 0,
				count1: 0,
				count2: 0,
				wrongList: [],
				cutVoiceSrc: '/static/audio/cut.mp3',
				yesVoiceSrc: '/static/audio/cut_yes.mp3',
				noVoiceSrc: '/static/audio/cut_no.mp3',
				// #ifdef APP
				popupCustomStyle: {},
				popupMask: true,
				// #endif
				// #ifdef H5
				popupCustomStyle: {'width': '430px','margin': '0 auto'},
				popupMask: true,
				// #endif
			}
		},
		onLoad(options) {
			if (!this.$store.state.isLogin) {
				uni.$u.toast('登录超时请重新登录')
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/login/login',
					});
				}, 2000)
				return false
			} else {
				if (options.studyId && options.currentPage && options.lastPage) {
					this.studyId = options.studyId
					this.boxPage = options.currentPage
					this.boxLastPage = options.lastPage
					this.boxData = []
					for (let i=0;i<this.boxPage;i++) {
						let temp = {}
						temp.page = i + 1
						temp.check = false
						this.boxData.push(temp)
					}
					this.getWords()
				} else {
					uni.navigateBack()
				}
			}
		},
		methods: {
			// 获取剪纸盒单词
			getWords() {
				this.$u.post(this.$u.api.studyCheck, {
					studyId: this.studyId,
					pages: this.boxPage,
					voiceType: this.$store.state.voiceType,
				}).then((res) => {
					let data = res.data
					this.wordsList = []
					this.list =[]
					for (let i=0;i<data.length;i++) {
						let temp = data[i]
						temp.show = 2
						temp.slide = false
						temp.change = false
						this.wordsList.push(temp)
						this.list.push(temp)
					}
					this.mixtureShow = false
				}).catch((res) => {
					console.log(res)
				})
			},
			// 剪单词
			checkWord(i, n) {
				if (n == 0) {
					this.list[i].change = true
					this.utils.playAudio(this.cutVoiceSrc)
					if (this.list[i].show == 2) {
						this.list[i].show = 1
						return false
					} else {
						this.list[i].slide = true
						this.count0++
					}
					
					if (this.count0 >= this.list.length) {
						// uni.$u.toast('检查完毕')
						setTimeout(() => {
							for (let i=0;i<this.list.length;i++) {
								this.list[i].show = 2
								this.list[i].slide = false
								this.list[i].change = false
							}
							this.times = 2
							this.count1 = 0
							this.count2 = 0
						}, 1000)
					}
				} else if (n == 1) {
					this.list[i].slide = true
					this.utils.playAudio(this.yesVoiceSrc)
					this.count1++
				} else if (n == 2) {
					this.list[i].slide = true
					this.utils.playAudio(this.noVoiceSrc)
					this.count2++
					this.wrongList.push(this.list[i])
				} else {
					return false
				}
				
				setTimeout(() => {
					this.list[i].show = false
				}, 500)
				
				// 没有错的进入混检
				if (n > 0 && this.count1 + this.count2 == this.wordsList.length && this.count2 == 0) {
					this.type = 2
				}
			},
			// 错的再剪
			wrongAgain() {
				if (this.count2 > 0) {
					this.list = []
					for (let i=0;i<this.wrongList.length;i++) {
						let temp = this.wrongList[i]
						temp.show = true
						temp.slide = false
						this.list.push(temp)
					}
					this.count2 = 0
					this.wrongList = []
				}
			},
			// 重新检查
			recheck() {
				this.list = []
				for (let i=0;i<this.wordsList.length;i++) {
					let temp = this.wordsList[i]
					temp.show = 2
					temp.slide = false
					this.list.push(temp)
				}
				this.type = 1
				this.times = 1
				this.count0 = 0
				this.count1 = 0
				this.count2 = 0
			},
			// 选择剪纸盒
			checkBox(i) {
				this.boxData[i].check = !this.boxData[i].check
			},
			// 开始检测
			startCheck() {
				let pages = []
				for (let i=0;i<this.boxData.length;i++) {
					let item = this.boxData[i]
					if (item.check) {
						pages.push(item.page)
					}
				}
				if (pages.length == 0) {
					uni.$u.toast('请选择检测单词组')
					return false
				}
				this.$u.post(this.$u.api.studyCheck, {
					studyId: this.studyId,
					pages: pages.join(','),
				}).then((res) => {
					// 修改页面标题
					uni.setNavigationBarTitle({
						title: '混组检测'
					})
					
					let data = res.data
					this.wordsList = []
					this.list =[]
					for (let i=0;i<data.length;i++) {
						let temp = data[i]
						temp.show = 2
						temp.slide = false
						this.wordsList.push(temp)
						this.list.push(temp)
					}
					this.mixtureShow = false
					this.type = 1
					this.times = 1
					this.count0 = 0
					this.count1 = 0
					this.count2 = 0
				}).catch((res) => {
					console.log(res)
				})
			},
			// 下一组
			nextPage() {
				uni.navigateBack({
					delta: 1,
					success: () => {
						uni.$emit('nextPage', this.boxPage)
					}
				})
			},
			// 结束识记
			endStudy() {
				if (this.boxPage != this.boxLastPage) {
					uni.$u.toast('还有单词未学习')
					return false
				}
				uni.navigateBack({
					delta: 1,
					success: () => {
						uni.$emit('endStudy')
					}
				})
			},
			// 发音
			playAudio(audio) {
				if (audio) {
					this.utils.playAudio(audio)
				}
			},
		},
	}
</script>

<style lang="less">
	page {
		background-color: #EEFAFF;
	}
	
	.parperbox_top {
		margin-bottom: 60px;
	}

	.parperbox_top_item {
		width: calc(100% - 60rpx);
		height: 120rpx;
		padding: 20rpx 30rpx;
		margin: 20rpx 30rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		box-sizing: border-box;
	}
	.slide_out {
		transition: transform 0.5s ease-in-out;
		transform: translateX(-120%);
	}

	.parperbox_top_item_yes {
		color: #FFFFFF;

		.parperbox_top_item_box1 {
			width: 70rpx;
			height: 70rpx;
			background: linear-gradient(250deg, #50C36D, #22B783);
			border-radius: 50%;
		}

		.parperbox_top_item_box2 {
			width: 70rpx;
			height: 70rpx;
			background: linear-gradient(250deg, #F77432, #F85E42);
			border-radius: 50%;
		}
		
		image{
			width: 50rpx;
			margin-left: 20rpx;
		}
	}
	.cropp_icon{
		width: 70rpx;
		height: 70rpx;
		image{
			width: 70rpx;
			height: 70rpx;
		}
	}
	.parperbox_bot{
		width: 100%;
		height: 120rpx;
		padding: 20rpx 30rpx;
		background-color: #ffffff;
		box-sizing: border-box;
		position: fixed;
		bottom: 0;
		left: 0;
	}
	.parperbox_bot_box{
		display: flex;
		flex-direction: row;
		align-items: center;
		// justify-content: space-between;
		justify-content: center;
		flex-wrap: wrap;
		
		.parperbox_bot_box_img{
			width: 80rpx;
			height: 80rpx;
			margin: 0 20rpx;
			box-sizing: border-box;
			
			image{
				display: block;
				width: 100%;
				height: 100%;
				margin: 0 auto;
			}
		}
		.parperbox_bot_box_image{
			width: 180rpx;
			height: 180rpx;
			padding: 30rpx;
			box-sizing: border-box;
			
			image{
				display: block;
				width: 100%;
				height: 100%;
				margin: 0 auto;
			}
		}
	}
	.delection-box{
		width: 100%;
		height: 80vh;
		
		.box{
			width: 33.33%;
			display: flex;
			flex-direction: column;
			align-items: center;
			cursor: pointer;
			
			image{
				width: 188rpx;
				height: 188rpx;
				margin-bottom: 10rpx;
			}
			.box-text{
				font-size: 28rpx;
				image{
					width: 26rpx;
					height: 26rpx;
					margin: 0;
				}
			}
			.text1{
				color: #FF6010;	
			}
			.text2{
				color: #4BB0FF;
			}
		}
	}
	.parperbox_age_til{
		padding-top: 60rpx;
		text-align: center;
		font-weight: bold;
		font-size: 36rpx;
		color: #333333;
	}
	.parperbox_age_but{
		width: 440rpx;
		height: 100rpx;
		background: #0B99FE;
		border-radius: 50rpx;
		font-weight: bold;
		font-size: 30rpx;
		color: #FFFFFF;
		line-height: 100rpx;
		margin: 10rpx auto 60rpx;
		text-align: center;
	}
</style>
